package com.sunelec.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunelec.pojo.Collector;
import com.sunelec.pojo.Inverter;
import com.sunelec.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 郑明利
 * @Description:电站设备信息
 * @Date: Create in 13:34 2018/05/10
 * @Modified By:
 */
@Controller
public class DeviceInfoController {

    @Autowired
    private DeviceInfoService deviceInfoService;

    /**
     * 根据powerId获得电站中采集器和逆变器中的数据
     * @param powerId
     * @param model
     * @return
     */
    @RequestMapping("/showDeviceInfo")
    public String ShowDeviceInformation(String powerId, Model model, @RequestParam(value="pn",defaultValue="1") Integer pn){

        PageHelper.startPage(pn,10);
        List<Collector> collectorList = deviceInfoService.getCollectInfoByPowerId(powerId);
        PageInfo<Collector> page = new PageInfo<Collector>(collectorList,1);
        model.addAttribute("pageInfo",page);
        model.addAttribute("collectorList",collectorList);
        model.addAttribute("powerId",powerId);

        ArrayList<Inverter> inverterList = new ArrayList<>();
        ArrayList<Inverter> errorList = new ArrayList<>();
        Inverter inverter = new Inverter();
        Inverter inverterError = new Inverter();
        for(int i=0;i<collectorList.size();i++){
            try{
                inverter = deviceInfoService.getInverterByCollectorSN(collectorList.get(i).getCollectorSn());
                if(inverter == null|| inverter.equals(null)){
                    inverterError.setCollectorSn(collectorList.get(i).getCollectorSn());
                    errorList.add(errorList.size(),inverterError);
                }else {
                    inverterList.add(inverterList.size(),inverter);
                }
            }catch (Exception e){
                inverterError.setCollectorSn(collectorList.get(i).getCollectorSn());
                errorList.add(errorList.size(),inverterError);
            }
        }
        model.addAttribute("inverterList",inverterList);
        model.addAttribute("errorList",errorList);
        return "deviceInformation";
    }
}